//
//  UserTicketModel.swift
//  shaohuo
//  用户票据
//  Created by MAYING on 15/12/27.
//  Copyright © 2015年 lchenc3. All rights reserved.
//

import UIKit

class UserNotificationSettingModel: NSObject {
    static let sharedInstance = UserNotificationSettingModel()
    fileprivate override init() {
        
    }
    
    //语音开关
    var is_voice_off : Bool? {
        set {
            
            UserDefaults.standard.set(newValue!, forKey: "USER_IS_VOICE_ON")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.bool(forKey: "USER_IS_VOICE_ON")
        }
    }
    
    //声音类型
    var is_voice_sex: String? {
        set {
            UserDefaults.standard.set(newValue!, forKey: "USER_IS_VOICE_SEX")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_IS_VOICE_SEX")
        }
    }
}

class UserTicketModel: NSObject {
    
    static let sharedInstance: UserTicketModel = UserTicketModel()

	fileprivate override init() {

	}
    
    var init_UUID: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_UUID")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_UUID")
        }
    }
    
    var perm: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_PERM")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            if let serverPerm = UserDefaults.standard.object(forKey: "USER_PERM") {

                let stringLength = (serverPerm as! NSString).length
                let permPart1 = (serverPerm as! NSString).substring(from: stringLength-128)
                let permPart2 = (serverPerm as! NSString).substring(to: stringLength-128)

                if isLogin {

                    UserDefaults.standard.set(("\(permPart1)\(permPart2)"), forKey: "ShaoHuo_KEY")
                    
                    return serverPerm as? String
                }
                
                UserDefaults.standard.set(serverPerm, forKey: "ShaoHuo_KEY")
                
                return ("\(permPart1)\(permPart2)")
            }
            return nil
        }
    }

    var time: NSNumber? {
        set {
            UserDefaults.standard.set(newValue!.intValue, forKey: "USER_TIME")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.integer(forKey: "USER_TIME") as NSNumber?
        }
    }
    // 用户编号
    var uid: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_ID")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_ID")
        }
    }
    /// 用户手机号码
    var phone: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_PHONE")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_PHONE")
        }
    }
    /// 用户图像
    var headerImg: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_HEADER_IMAGE")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_HEADER_IMAGE")
        }
    }

    /// 经纬度
    var lat: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_LAT")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_LAT")
        }
    }
    var lng: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_LNG")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_LNG")
        }
    }
    
    var alipay_account: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_AlipayID")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_AlipayID")
        }
    }
    // 用户Token
    var token: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_TOKEN")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_TOKEN")
        }
    }
    /// 用户类型
    var user_type: Int? {
        set {
            UserDefaults.standard.set(newValue!, forKey: "USER_TYPE")
            UserDefaults.standard.synchronize()
        }
        get {
            return UserDefaults.standard.integer(forKey: "USER_TYPE")
        }
    }
    /// 用户子类型
    var sub_user_type: Int? {
        set {
            UserDefaults.standard.set(newValue!, forKey: "USER_SUB_TYPE")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.integer(forKey: "USER_SUB_TYPE")
        }
    }
    
    var user_level: Int? {
        set {
            UserDefaults.standard.set(newValue!, forKey: "USER_LEVEL")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.integer(forKey: "USER_LEVEL")
        }
    }
    
    /// 我是否是捎货人
    var meIsShipper: Bool {
        get {
            let usertype = self.user_type ?? 0
            if usertype & 1 == 1 {
                return true
            }
            return false
        }
    }
    /// 普通用户
    var normalUser: Bool {
        get {
            let usertype = self.user_type ?? 1
            if usertype == 1 {
                return true
            }
            return false
        }
    }
    /// 捎货人状态 YES： 接单中, FALSE： 休息中
    var shipping_status: Bool {
        get {
            return UserDefaults.standard.bool(forKey: "USER_SHIPPING_STATUS")
            
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_SHIPPING_STATUS")
        }
    }
    ///捎货人被禁止接单状态
    var shipper_fobidden_status: Int? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_FOBIDDEN_STATUS")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.integer(forKey: "USER_FOBIDDEN_STATUS")
        }
    }
    /// 捎货人禁止接单提示
    var shipper_fobidden_tips: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_FOBIDDEN_TIPS")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_FOBIDDEN_TIPS")
        }
    }
    
    /// 环信登录账号
    var easemobUserName: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "EASEMOB_USER_NAME")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "EASEMOB_USER_NAME")
        }
    }
    /// 环信登录密码
    var easemobPassword: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "EASEMOB_USER_PWD")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "EASEMOB_USER_PWD")
        }
    }
    /// 当前城市
    var currentCity: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_CURRENT_CITY")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_CURRENT_CITY")
        }
    }
    /// 当前城市编号
    var currentCityId: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_CURRENT_CITY_ID")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_CURRENT_CITY_ID")
        }
    }
    /// 用户所属行业
    var user_default_industy: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_DEFAULT_INDUSTRY")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_DEFAULT_INDUSTRY")
        }
    }
    
    var nick: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_DEFAULT_NICK")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_DEFAULT_NICK")
        }
    }
    
    var name: String? {
        set {
            UserDefaults.standard.set(newValue, forKey: "USER_DEFAULT_NAME")
            //NSUserDefaults.standardUserDefaults().synchronize()
        }
        get {
            return UserDefaults.standard.string(forKey: "USER_DEFAULT_NAME")
        }
    }
    
    var isLogin: Bool {
        get {
            return self.uid == nil ? false : true
        }
    }
    
    var isEaseMobLogin: Bool = false
    
    // 退出登录
    func logout() -> Void {
        UserDefaults.standard.removeObject(forKey: "USER_ID")
        UserDefaults.standard.removeObject(forKey: "USER_TOKEN")
        UserDefaults.standard.removeObject(forKey: "SEND_ADDRESS_ENTITY")
        UserDefaults.standard.removeObject(forKey: "RECV_ADDRESS_ENTITY")
        UserDefaults.standard.removeObject(forKey: "EASEMOB_USER_NAME")
        UserDefaults.standard.removeObject(forKey: "EASEMOB_USER_PWD")
        UserDefaults.standard.removeObject(forKey: "USER_DEFAULT_NICK")
        UserDefaults.standard.removeObject(forKey: "USER_DEFAULT_NAME")
        //NSUserDefaults.standardUserDefaults().removeObjectForKey("perm")
        ////NSUserDefaults.standardUserDefaults().synchronize()
        
    }
}
